/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "Player.h"
#include "Channel.h"
#include "Maps.h"
#include "Map.h"
#include "Event.h"
#include "Events.h"
#include "../client/EventsData.h"
#include "../client/EventData.h"
#include "Players.h"
#include "../net/world/Party.h"
#include "../tools/Tools.h"
#include "../tools/Timer.h"

Event* Events::getEvent(string name){
	if(events.find(name) != events.end())
		return events[name];
	return 0;
}

bool Events::createEvent(string name){
	EventData* eventd = EventsData::getInstance()->getDataByID(name);
	if(eventd == 0)
		return false;
	if(getEvent(name) != 0)
		return false;
	if(last.find(name) != last.end() && last[name] > Tools::getServerTime() - 10000)
		return false;
	events[name] = new Event(channel, name);
	return true;
}

bool Events::addPlayer(string name, Player* player){
	Event* pevent = getEvent(name);
	if(pevent == 0)
		return false;
	pevent->start(player);
	return true;
}

void Events::deleteEvent(string name, bool timer){
	Event* pevent = getEvent(name);
	if(pevent == 0)
		return;
	if(pevent->getDeleted())
		return;
	pevent->setDeleted(true);
	pevent->end(timer);
	vector <int>* maps = EventsData::getInstance()->getDataByID(name)->getMapsData()->getData();
	for(int i=0; i<(int)maps->size(); i++){
		Map* map = channel->getMaps()->getMap((*maps)[i]);
		if(map != 0) {
			map->setEvent(0);
		}
	}
	last[name] = Tools::getServerTime();
	if(!timer) {
		Timers::getInstance()->cancelTimer(pevent->getTimer());
		//Timer::Id id(Timer::Types::EventTimer, pevent->getId(), 0);
		//pevent->getChannel()->getTimers()->removeTimer(id);
	}
	events.erase(name);
	delete pevent;
}

void Events::deleteEventForPlayer(string name, Player* player, bool logoff){
	Event* pevent = getEvent(name);
	if(pevent == 0)
		return;
	if(pevent->getDeleted()) 
		return;
	if(player->getParty() != 0 && pevent->getParty() && player->getParty()->getLeader() == player->getID()){
		deleteEvent(pevent->getName());
		return;
	}
	if(pevent->getPlayers()->getPlayersCount() == 1){
		vector <int>* maps = EventsData::getInstance()->getDataByID(name)->getMapsData()->getData();
		for(int i=0; i<(int)maps->size(); i++){
			Map* map = channel->getMaps()->getMap((*maps)[i]);
			if(map != 0){
				map->setEvent(0);
			}
		}
	}
	pevent->end(player, !logoff);
	if(pevent->getPlayers()->getPlayersCount() == 0){
		last[name] = Tools::getServerTime();
		Timers::getInstance()->cancelTimer(pevent->getTimer());
		//Timer::Id id(Timer::Types::EventTimer, pevent->getId(), 0);
		//player->getChannel()->getTimers()->removeTimer(id);
		events.erase(name);
		delete pevent;
	}
}